<?php 
/**
 * 
 * 用户信息 处理类
 * @author tanzhe
 *
 */
class UserinfoModel {
	
	private $cache;
	private $db;
	
	public function __construct(){
		$this->cache = new Cache();
		$this->db = new Data();
	}
	
	public function  get($key) { //获取用户信息
		$userId = $this->cache->get($key);
	 	if (!$userId) {
	  		return array('code'=>2, 'ret'=>'');
		}
		$query = $this->db->query("select * from player_info where `u_id`={$userId}");
		$playerInfo = $this->db->fetch_array($query);
		//工作
		if ( !empty($playerInfo['job']) ) {
			$playerInfo['job'] = unserialize($playerInfo['job']);
		}
		//衣服
		if ( !empty($playerInfo['clothes']) ) {
			$playerInfo['clothes'] = unserialize($playerInfo['clothes']);
		}
		$time = time();
		//处理体力值
		$updateData = array();
		global $fillEnergyCdTime;
		if ( $playerInfo['now_energy'] < $playerInfo['max_energy'] ) {
			$energyCd = floor($fillEnergyCdTime/(1+$playerInfo['cd_factor']/100));
			$fillNum = floor (($time - $playerInfo['last_energy_time']) / $energyCd);
			if ( $fillNum > 0 ) {
				$updateData['now_energy'] = $playerInfo['now_energy'] + $fillNum;
				if ( $updateData['now_energy'] > $playerInfo['max_energy']  ) {
					$updateData['now_energy'] = $playerInfo['max_energy'];
				}
				$updateData['last_energy_time'] = $playerInfo['last_energy_time'] + $energyCd * $fillNum;
				$updateData['next_energy_time'] = $updateData['last_energy_time'] + $energyCd;
			}
		}
		if ( count($updateData) > 0 ){
			$options = array(
				'key'=>array(
					'u_id'=>$playerInfo['u_id'],
				),
				'data'=>$updateData,
			);
			$this->db->updateData('player_info', $options);
			$playerInfo = array_merge($playerInfo, $updateData);
		}
		$this->cache->set("ku_".$key, $playerInfo);
		//处理到期buff
		$itemModel = new ItemModel();
		$itemModel->endBuff($key);
		//重新取数据
		$playerInfo = $this->cache->get('ku_'.$key);
		//cd时间
		$countDown = 0;
		if ( $playerInfo['now_energy'] < $playerInfo['max_energy'] ) {
			$countDown = $playerInfo['next_energy_time'] - $time;
		}
		$ret = array(
			'p_nickname'=>$playerInfo['p_nickname'],
			'coin'=>$playerInfo['coin'],
			'cash'=>$playerInfo['cash'],
			'now_energy'=>$playerInfo['now_energy'],
			'max_energy'=>$playerInfo['max_energy'],
			'hanger'=>$playerInfo['hanger'],
			'coin_factor'=>$playerInfo['coin_factor'],
			'hanger_factor'=>$playerInfo['hanger_factor'],
			'cd_factor'=>$playerInfo['cd_factor'],
			'count_down'=>$countDown,
			'clothes'=>$playerInfo['clothes'],
			'style'=>$playerInfo['style'],
			'color'=>$playerInfo['color'],
			'skin'=>$playerInfo['skin'],
			'work_hotness'=>$playerInfo['work_hotness'],
			'club_hotness'=>$playerInfo['club_hotness'],
			'job'=>$this->filterJob($playerInfo['job']),
			'my_clothes'=>$this->getAllClothes($key, 0),
		);
		//任务
		$taskModel = new TaskModel();
		$taskModel->getTask($key, 0);
		$taskModel->isFinish($key);
		$taskRecord = $this->cache->get('task_record_'.$playerInfo['u_id']);
		
		$ret['task_status'] = $taskRecord;
		//男朋友信息
		if ( !empty($playerInfo['boyfriend_id']) ) {
			$boyFriendModel = new BoyfriendModel();
			$boyFriendInfo = $boyFriendModel->get($key, 0);
			if ( $boyFriendInfo['code'] == 0 ) {
				//$ret['boyfriend_info'] = $boyFriendInfo['ret'];
				if ( isset($boyFriendInfo['ret']['exceed']) ) {
					$ret['exceed'] = 1;
				} else {
					$ret['boyfriend_info'] = $boyFriendInfo['ret'];
				}
			} 
		}
		return array('code'=>0, 'ret'=>array("user"=>$ret));
	}
	
	public function  show($key) { //查看用户信息
		$userId = $this->cache->get($key);
	 	if (!$userId) {
	  		return array('code'=>2, 'ret'=>'');
		}
		$playerInfo = $this->cache->get("ku_".$key);
		print_r($playerInfo);
		//男朋友信息
		if ( !empty($playerInfo['boyfriend_id']) ) {
			$boyfriendInfo = $this->cache->get("boyfriend_".$playerInfo['boyfriend_id']);
			print_r($boyfriendInfo);
		}
	}
	
	public function getAllClothes($key, $cache = 1) { //获取用户已经拥有的所有衣服
		$userId = $this->cache->get($key);
		$playerClothes = $cache == 1 ? $this->cache->get('my_clothes_'.$userId) : array();
		$playerClothes = array();
		if ( empty($playerClothes) ) {
			$query = $this->db->query("select `label`,`hotness` from player_clothes where `u_id`={$userId} order by hotness,hotness_type desc");
			$clothesInfo = $this->db->fetchAllByQuery($query);
			$clothesInfo = empty($clothesInfo) ? array() : $clothesInfo;
			$playerClothes = array();
			foreach ( $clothesInfo as $v ) {
				$playerClothes[$v['label']] = $v['hotness'];
			}
			$this->cache->set('my_clothes_'.$userId, $playerClothes);
		}
		return $playerClothes;
	}
	
	public function fillEnergy($key) { //恢复体力
		$playerInfo = $this->cache->get('ku_'.$key);
		
		$chkRet = Common::checkPlayerInfo($playerInfo);
		if($chkRet != 0) {
			return array('code'=>$chkRet,'ret'=>'');
		}
		
		if ( $playerInfo['now_energy'] >= $playerInfo['max_energy'] ) {
			return array('code'=>215,'ret'=>''); 
		}
		$time = time();
		if ( ($playerInfo['next_energy_time'] - $time) > 3 ) { 
			/*$ret = array(
				'count_down'=>$playerInfo['next_energy_time'] - $time,
			);*/
			return array('code'=>230,'ret'=>$playerInfo['next_energy_time'] - $time); 
		}
		$updateData = array();
		$updateData['now_energy'] = $playerInfo['now_energy'] + 1;
		$updateData['last_energy_time'] = $time;
		global $fillEnergyCdTime;
		$updateData['next_energy_time'] = $time + floor($fillEnergyCdTime/(1+$playerInfo['cd_factor']/100));
		$playerInfo = array_merge($playerInfo, $updateData);
		$this->cache->set("ku_".$key, $playerInfo);
		//更新信息
		$options = array(
			'key'=>array(
				'u_id'=>$playerInfo['u_id'],
			),
			'data'=>$updateData,
		);
		$this->db->updateData('player_info', $options);
		$countDown = 0;
		if ( $playerInfo['now_energy'] < $playerInfo['max_energy'] ) {
			$countDown = $updateData['next_energy_time'] - $time;
		}	
		$ret = array(
			'now_energy'=>$playerInfo['now_energy'],
			'count_down'=>$countDown,
		);
		return array('code'=>0,'ret'=>$ret); 
	}
	
	public function fillEnergyByCash($key) { //通过cash直接恢复体力
		$playerInfo = $this->cache->get('ku_'.$key);
		$chkRet = Common::checkPlayerInfo($playerInfo);
		if($chkRet != 0) {
			return array('code'=>$chkRet,'ret'=>'');
		}
		if ( $playerInfo['now_energy'] >= $playerInfo['max_energy'] ) {
			return array('code'=>215,'ret'=>''); 
		}
		//需要的元宝
		$needCash = $playerInfo['max_energy'] - $playerInfo['now_energy'];
		//判断元宝是否足够
		if ( $needCash > $playerInfo['cash'] ) {
			return array('code'=>10,'ret'=>'');
		}
		$time = time();
		$updateData = array();
		$updateData['now_energy'] = $playerInfo['max_energy'];
		$updateData['cash'] = $playerInfo['cash'] - $needCash;
		$playerInfo = array_merge($playerInfo, $updateData);
		$this->cache->set("ku_".$key, $playerInfo);
		//更新信息
		$options = array(
			'key'=>array(
				'u_id'=>$playerInfo['u_id'],
			),
			'data'=>$updateData,
		);
		$this->db->updateData('player_info', $options);
		//写元宝log
		if ( $needCash > 0 ) {
			$options = array(
				'u_id'=> $playerInfo['u_id'],
				'type'=> 'fill_energy',
				'num'=> -$needCash,
				'data'=> '',
				'add_time'=> time(),
			);
			Common::cashLog($options);
		}
		$countDown = 0;
		$ret = array(
			'now_energy'=>$playerInfo['now_energy'],
			'cash'=>$playerInfo['cash'],
			'count_down'=>$countDown,
		);
		return array('code'=>0,'ret'=>$ret); 
	}
	
	public function defaultClothes($userId) { //注册时送的衣服
		global $defaultRegInfo;
		$aryClothes = $defaultRegInfo['clothes'];
		//验证必穿项
		//1、连衣裙 2、外衣 3、内衣 4、裙子裤子 5、首饰 6、挎包 7、鞋子
		if ( empty($aryClothes[7]) ) {
			return array('code'=>216,'ret'=>'');
		}
		if ( empty($aryClothes[1]) ) {
			if ( empty($aryClothes[3]) ) {
				return array('code'=>217,'ret'=>'');
			}
			if ( empty($aryClothes[4]) ) {
				return array('code'=>218,'ret'=>'');
			}
		}
		//判断互斥
		if ( !empty($aryClothes[1]) ) {
			if ( !empty($aryClothes[3]) ) {
				return array('code'=>219,'ret'=>'');
			}
			if ( !empty($aryClothes[4]) ) {
				return array('code'=>220,'ret'=>'');
			}
		}
		$workHotness = 0;
		$clubHotness = 0;
		$clothesLabel = array();
		$hanger = 0;
		foreach ( $aryClothes as $v ) {
			if ( !empty($v) ) {
				$clothesData = $this->cache->get('clothes_'.$v);
				if ( empty($clothesData) ) {
					$query = $this->db->query("select * from clothes where `label`='{$v}'");
					$clothesData = $this->db->fetchOneByQuery($query);
					$this->cache->set('clothes_'.$v, $clothesData);
				}
				//插入衣服信息
				$insertData = array(
					'u_id'=>$userId,
					'label'=>$v,
					'hotness_type'=>$clothesData['hotness_type'],
					'hotness'=>$clothesData['hotness'],
					'is_now'=>1,
				);
				$this->db->insertData('player_clothes', $insertData);
				$hanger++;
				if ( $clothesData['hotness_type'] == 1 ) {
					$workHotness += $clothesData['hotness'];
				} elseif ( $clothesData['hotness_type'] == 2 ) {
					$clubHotness += $clothesData['hotness'];
				} else {
					$workHotness += $clothesData['hotness'];
					$clubHotness += $clothesData['hotness'];
				}
				$clothesLabel[] = $v;
			}
		}
		//计算工作属性与夜店属性
		$updateData = array();
		$updateData['clothes'] = serialize($aryClothes);
		$updateData['work_hotness'] = $workHotness;
		$updateData['club_hotness'] = $clubHotness;
		$updateData['hanger'] = $hanger;
		//更新角色信息
		$options = array(
			'key'=>array(
				'u_id'=>$userId,
			),
			'data'=>$updateData,
		);
		$this->db->updateData('player_info', $options);
		return array('code'=>0,'ret'=>'');
	}
	
	private function filterJob($jobData) { //格式化工作记录
		$returnJob = array();
		if ( !empty($jobData) ) {
			foreach ( $jobData as $v ) {
				foreach ( $v as $k1=>$v1 ) {
					$returnJob[$k1] = $v1;
				}
			}
		}
		return $returnJob;
	}
	
	/**
	 * 
	 * 设置昵称
	 * str $key
	 * str $nickname 昵称
	 */
	public function setNickname($key, $nickname) {
		$playerInfo = $this->cache->get('ku_'.$key);
		$chkRet = Common::checkPlayerInfo($playerInfo);
		if($chkRet != 0) {
			return array('code'=>$chkRet,'ret'=>'');
		}
		
		$len = strlen($nickname);
		if ( $len < 2 OR $len > 16 ) {
			return array('code'=>234,'ret'=>''); 
		}
		//用户名字不能注册
		global $char_limit;
		foreach ($char_limit as $m){
			if(strstr($nickname, $m)) {
				return array('code'=>235, 'ret'=>'');
			}
		}
		global $no_user_array;
		foreach ($no_user_array as $m){
			if(strstr($nickname, $m)){
				return array('code'=>235, 'ret'=>'');
			}
		}
		$checkQuery = $this->db->query("select u_id from player_info where `p_nickname`='{$nickname}'");
		$checkResult = $this->db->fetch_array($checkQuery);
		if ($checkResult) {
			return array('code'=>236, 'ret'=>'');
		}
		$updateData = array();
		$updateData['p_nickname'] = $nickname;
		$playerInfo = array_merge($playerInfo, $updateData);
		$this->cache->set("ku_".$key, $playerInfo);
		//更新信息
		$options = array(
			'key'=>array(
				'u_id'=>$playerInfo['u_id'],
			),
			'data'=>$updateData,
		);
		$this->db->updateData('player_info', $options);
		$ret = array(
			'p_nickname'=>$nickname,
		);
		return array('code'=>0,'ret'=>$ret); 
	}
}
?>